#include "segment.h"
#include "data_processor.h"
#define LOG_SET_LOCAL_LEVEL LOG_LEVEL_NONE
#include "log.h"

__weak float32_t segment_data_format(uint32_t data) {
    
    LOG_debug("Orignal data: %x", data);
    data >>= 8;
    LOG_debug("After Offset data: %x", data);
    float32_t tmp = (float32_t)data;
    tmp -= 0x7FFFFFU;
    tmp /= 0xFFFFFFU;
    tmp *= 3.3F;
    LOG_debug("After change to float: %f", tmp);
    return tmp;
}

void segment_init(segment_t *thiz, float32_t *dataPtr, uint32_t no) {
    
    thiz->DataPtr = dataPtr;
    thiz->No = no;
    thiz->Len = 0U;
    thiz->Capacity = SEGMENT_CAPACITY;
    thiz->IsUsed = 0U;
    thiz->StInfo.DiffMaxABS = 0.0F;
    thiz->StInfo.ValMaxABS = 0.0F;
    thiz->StInfo.StdDev = 0.0F;
}

void segment_load(segment_t *thiz, const void *srcPtr, uint32_t len) {
    
    uint32_t *dataPtr = (uint32_t *)srcPtr;
    len = len > thiz->Capacity ? thiz->Capacity : len;
    for (uint32_t index = 0U; index < len; index++) {
        *(thiz->DataPtr + index) = segment_data_format(*(dataPtr + index));
    }
    thiz->Len = len;
}
